/**
 * 
 * @param $
 */

(function ($) {
    $.fn.extend({
        "numberKey":function(options){
        	var opts = $.extend({}, defaluts, options);
        	var name = this.attr("name");
        	var that = this;
        	var cls = "num_"+name; 
        	var container;
        	var input;
        	initKeyboard();
        	container.find(".body_num_key").on("click",function(){
        		var val = $(this).data("value");
        		if(val!="delete"){
        			numberKeyClick(this);
        		}else{
        			var str = ""+that.get();
        			that.set(str.substring(0,str.length-1));
        		}
        	});
        	var longtime;
        	container.find(".btn_delete").on("touchstart",function(){
        		longtime = setTimeout(function(){
        			clearNum()
        		},800);
        	}).on("touchmove",function(){
        		clearTimeout(longtime);
        	}).on("touchend",function(){
        		clearTimeout(longtime);
        	});
        	function clearNum(){
        		that.set("");
        	}
        	this.get = function(){//获取输入值
        		return that.val();
        	}
        	this.destory = function(){//销毁数字输入控件
        		container.remove();
        		var input = "num_keyboard_input_"+name;
        		$("."+input).remove();
        		that.show();
        	}
        	this.set = function(double){//设置输入值
        		if(!input.hasClass("process_input")){
        			input.addClass("process_input");
        		}
        		that.val(double);
        		if(double==""){
        			double=opts.placeholder;
        			if(input.hasClass("process_input")){
        				input.removeClass("process_input");
        			}
        		}
        		input.text(double);
        	}
        	function numberKeyClick(btn){
        		var btn = $(btn);
        		var num = btn.data("value");
        		var val = that.val();
        		var str = ""+val;
        		if(!isInt(num)){
        			if(isDouble(val) || !opts.allowDecimals){
        				return false;
        			}
        		}else{
        			if(isDouble(val)){
        				var dous = str.split(".");
        				if(dous[1].length>=opts.digits){
        					return false;
        				}
        			}
        		}
        		str+=num;
        		if(parseFloat(opts.max)<parseFloat(str)){
        			return false;
        		}
        		that.set(str);
        	}
        	function isDouble(num){
        		var str = ""+num;
        		var dous = str.split(".");
        		return (dous.length==2);
        	}
        	function isInt(num){
        		var str = ""+num;
        		return str.match(/^\d+$/);
        	}
        	function format(str,args) {
        		for (var s = str, i = 0; i < args.length; i++)
        			s = s.replace(new RegExp("\\{" + i + "\\}", "g"), args[i]);
        		return s;
        	}
        	function initKeyboard(){
            	var wrap = "<div class=\"num_keyboard_container "+cls+"\">\n";
            	var body="<div class=\"num_keyboard_body\">\n";
            	var number = "<div class=\"body_num_key {2}\" data-value=\"{0}\">{1}</div>";
            	var numbers = [];
            	for(var i=1;i<10;i++){
            		numbers.push(format(number,[i,i,""]));
            	}
            	numbers.push(format(number,[".",".","key_function"]));
            	numbers.push(format(number,["0","0",""]));
            	numbers.push(format(number,["delete","删除","key_function btn_delete"]));
            	body+=numbers.join("\n")+"</div>";
            	wrap+=body+"\n</div>";
            	$("body").append($(wrap));
            	container = $("."+cls);
            	var inputcls = "num_keyboard_input input_"+name;
            	input = $("<div class=\""+inputcls+"\"></div>");
            	that.hide().after(input);
            	input.on("click",function(){
            		container.show();
            	}).text(opts.placeholder).show();
        	}
        	return this;
        }
    });
    //默认参数
    var defaluts = {
        max: 10000*10000,//最大值 默认1亿
        allowDecimals:true,//允许输入小数true/false 默认true 允许
        placeholder:"请输入数字",//提示占位符
        digits:2//可输入几位小数
    };
})(window.jQuery);